package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.*;
import com.dongdong.service.*;
import com.dongdong.vo.SpecVO;
import com.dongdong.vo.TemplateVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("goodController")
public class GoodsController {
    @Reference
    private BrandService brandService;
    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDecService goodsDecService;
    @Reference
    private TypeTemplateService typeTemplateService;
    @Reference(timeout = 5000)
    private ItemService itemService;
    @Reference
    private SpecificationService specificationService;

    @RequestMapping("toGoodEdit")
    public String toGoodEdit(){
        return "admin/goods_edit";
    }
    @RequestMapping("catList")
    @ResponseBody
    public Result itemCatList(Long parentId){
        try{
            List<TbItemCat> itemCatList =  itemService.selectInfo(parentId);
            return Result.respone(ResultEmnu.SUCCESS).setData(itemCatList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("templatesList/{typeId}")
    @ResponseBody
    public Result templatesList(@PathVariable("typeId")Long typeId, Model model){
        try {
            TbTypeTemplate template = typeTemplateService.selectInfo(typeId);
            model.addAttribute("template",template);
            return Result.respone(ResultEmnu.SUCCESS).setData(template);
        }catch (Exception e){
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("selectItem")
    @ResponseBody
    public Result selectItem(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectById(id);
            return Result.respone(ResultEmnu.SUCCESS).setData(tbTypeTemplate);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("increase")
    @ResponseBody
    public Result increase(TbGoods tbGoods,TbGoodsDesc tbGoodsDesc,String tbItem){
        tbGoodsDesc.setSpecificationItems("[{'attributeName':'网络','attributeValue':['移动3G','移动4G']},{'attributeName':'机身内存','attributeValue':['16G','32G']}]");

       try{
           TbSeller tbSeller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
           tbGoods.setSellerId(tbSeller.getSellerId());
           String nickName = tbSeller.getNickName();
           List<TbItem> itemList = JSONObject.parseArray(tbItem,TbItem.class);
           itemList.get(0).setSpec("{'网络':'移动4G','机身内存':'32G'}");
           int i = goodsDecService.insertInfo(tbGoods,tbGoodsDesc,nickName,itemList);
           return Result.respone(ResultEmnu.SUCCESS).setData(i);
       }catch (Exception e){
           e.printStackTrace();
           return Result.respone(ResultEmnu.ERROR);
       }
    }
    @RequestMapping("templatesLists")
    @ResponseBody
    public Result templatesLists(Long typeId){

            TbTypeTemplate template = typeTemplateService.selectInfo(typeId);
            TemplateVo tv = new TemplateVo();
            tv.setTbTypeTemplate(template);
            List<Map> specList = JSONObject.parseArray(template.getSpecIds(), Map.class);
            List<TbSpecification> collect = specList.stream().map(map -> {
                Integer id = (Integer) map.get("id");
                String name = (String) map.get("text");
                TbSpecification spec = new TbSpecification();
                spec.setId(id.longValue());
                spec.setSpecName(name);
                return spec;
            }).collect(Collectors.toList());
        List<SpecVO> specVOList = collect.stream().map(s -> specificationService.selectById(s.getId())).collect(Collectors.toList());
        tv.setSpecVOList(specVOList);
        return Result.respone(ResultEmnu.SUCCESS).setData(tv);



    }


}
